<?php
/*
 *@Note  管理员控制器
 * @Date 2022-06
 * @aurh wangmian
 */

namespace app\system\controller;


class Admin extends Base
{
    /**
     * @Notes  管理员列表
     * @param $data
     * @return array
     */
    public function index() {
        if(request()->isPost()){
            $pageSize = input('limit/d');
            $key = input('key','');
            $group_id = input('group_id/d',0);
            $where = [];
            if (session('aid') != 1)$where['id'] = session('aid');
            if($key) $where['username'] = ['like','%'.$key.'%'];
            if($group_id) $where['group_id'] = $group_id;
            $list = model('Admin')->where($where)->order('id asc')->paginate($pageSize)->toArray();
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['title'] = model('AuthGroup')->where('group_id',$v['group_id'])->value('title');
                $list['data'][$k]['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
            }
            return ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        // 用户组
        $authgruop = model('auth_group')->field('group_id,title')->select();
        $this->assign('authgruop',$authgruop);
        return $this->fetch();
    }
    /**
     * @Notes  管理员添加
     * @param $data
     * @return array
     */
    public function add() {
        if(request()->isPost()){
            $data = input('post.');
//            if (!preg_match("/^[A-Za-z][0-9A-Za-z]{4,}$/", $data['username'])) {
//                return ajaxArray(0,'用户名以字母开头，用字母加数字，最小5位');
//            }
            if (!preg_match("/^\S{6,15}$/", $data['password'])) {
                return ajaxArray(0,'密码长度6-15位');
            }
            if (!preg_match("/^1[34578]\d{9}$/", $data['phone'])) {
                return ajaxArray(0,'手机格式错误');
            }
            $count = model('admin')->where(['username'=>$data['username']])->count();
            if($count){
                return ajaxArray(0,'用户已存在，请重新输入用户名!');
            }
            $data['password'] = md5($data['password']);
            $data['last_login_time'] = $data['add_time'] = time();
            $data['last_login_ip'] = request()->ip();
            $result = model('admin')->insert($data);
            if ($result !== false) {
                return ajaxArray(1,'管理员添加成功');
            }
            return ajaxArray(0,'管理员添加失败');
        }else{
            $auth_group = model('auth_group')->select();
            $this->assign('authGroup',$auth_group);
            return $this->fetch();
        }
    }
    //更新管理员信息
    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
//            if (!preg_match("/^[A-Za-z][0-9A-Za-z]{4,}$/", $data['username'])) {
//                return ajaxArray('用户名以字母开头，用字母加数字，最小5位');
//            }
            $count = model('admin')->where(['username'=>$data['username'],'id'=>['neq',$data['id']]])->count();
            if($count){
                return ajaxArray(0,'用户已存在，请重新输入用户名!');
            }
            if(empty($data['password'])){
                unset($data['password']);
            }else{
                if (!preg_match("/^\S{6,15}$/", $data['password'])) {
                    return ajaxArray(0,'密码长度6-15位');
                }
                $data['password'] = md5($data['password']);
            }
            if (!preg_match("/^1[34578]\d{9}$/", $data['phone'])) {
                return ajaxArray(0,'手机格式错误');
            }

            $result = model('admin')->update($data);
            if ($result !== false) {
                return ajaxArray(1,'管理员修改成功');
            }
            return ajaxArray(0,'管理员修改失败');
        }else{
            $id = input('id/d',0);
            $this->assign('id', $id);
            $info = model('Admin')->get($id);
            $auth_group = model('auth_group')->select();
            $this->assign('info', $info);
            $this->assign('authGroup',$auth_group);
            return $this->fetch();
        }
    }
    //修改管理员状态
    public function state(){
        $id = input('post.id/d',0);
        $status = model('admin')->where('id',$id)->value('status');
        $status = $status ? 0 : 1;
        $result = model('admin')->where('id',$id)->setField('status', $status);
        if ($result !== false) {
            return ajaxArray(1,'编辑成功',$status);
        }
        return ajaxArray(0,'编辑失败');
    }
    //删除管理员
    public function del(){
        $id = input('id/d',0);
        return ajaxArray(1,'删除成功',$id);
        if (session('aid')==1){
            $result = model('admin')->destroy($id);
            if ($result !== false) {
                return ajaxArray(1,'删除成功!');
            }else{
                return ajaxArray(0,'删除失败!');
            }
        }else{
            return ajaxArray(0,'您没有删除管理员的权限!');
        }
    }
}